#pragma once
// Tonokip RepRap firmware rewrite based off of Hydra-mmm firmware.
// Licence: GPL

#include "Configuration.h"

#if PRINTER_TYPE != PRINTER_MAXI_SLAVE

//==========================================================

#include <inttypes.h>
#include "util.h"

//extern "C" void __cxa_pure_virtual();

#ifdef __cplusplus
	extern "C" {
#endif
//==========================================================

#define X_AXIS 0
#define Y_AXIS 1
#define Z_AXIS 2
#define E_AXIS 3


// This struct is used when buffering the setup for each linear movement "nominal" values are as specified in 
// the source g-code and may never actually be reached if acceleration management is active.
typedef struct block
{
	struct block * next;
	struct block * prev;
	
	// Fields used by the bresenham algorithm for tracing the line
	int32_t steps_x;
	int32_t steps_y;
	int32_t steps_z;
	int32_t steps_e;  

	int32_t step_event_count;     // The number of step events required to complete this block
	int32_t accelerate_until;     // The index of the step event on which to stop acceleration
	int32_t decelerate_after;     // The index of the step event on which to start decelerating
	int32_t acceleration_rate;    // The acceleration rate used for acceleration calculation
	int8_t out_bits;				// The direction bit set for this block (refers to *_DIRECTION_BIT in config.h)
  
	volatile int8_t busy : 1;
	int8_t recalculate_trapezoid_flag : 1;            // Planner flag to recalculate trapezoids on entry junction
    
	// Settings for the trapezoid generator
	uint16_t nominal_rate;                                 // The nominal step rate for this block in step_events/sec 
	uint16_t initial_rate;								 // The jerk-adjusted step rate at start of block  
	uint16_t final_rate;									 // The minimal rate at exit
  
	// Pomocne promenne pro planovac
	float allowable_initial_rate;						 // maximalni rychlost na zacatku ze ktere jsem schopen zpomalit na final_rate
	float max_initial_rate;							 // maximalni pocateni rychlost s oheldem na jerk
	int32_t acceleration_st;                              // acceleration steps/sec^2
	float _2ad;
  
} block_t;


void FlushSerialRequestResend();
void ClearToSend();


void manage_inactivity();

void get_command();
void get_coordinates();
void prepare_move();
void prepare_arc_move(char isclockwise);


void kill();

void check_axes_activity();
void plan_init();
void st_init();
void tp_init();
void plan_buffer_line(float x, float y, float z, float e, float feed_rate);
void plan_set_position(float x, float y, float z, float e);
void st_wake_up();
void st_synchronize();
//void st_set_position(const int32_t &x, const int32_t &y, const int32_t &z, const int32_t &e);

void check_buffer_while_arc();

#ifdef SDSUPPORT
void print_disk_info(void);
#endif //SDSUPPORT


extern void SprinterSetup();
extern void SprinterLoop();

#ifdef DEBUG
void log_message(char*   message);
void log_bool(char* message, bool value);
void log_int(char* message, int value);
void log_long(char* message, long value);
void log_float(char* message, float value);
void log_uint(char* message, unsigned int value);
void log_ulong(char* message, unsigned long value);
#endif

#ifdef __cplusplus
	}
#endif

#endif